﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeChatParking.Entity;

namespace WeChatParking.Core.BLL
{
    public class User_info
    {
        public static List<Entity.User_info> Get(string filter, string OrderFields = "", bool isDesc = false)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    string sql = "select * from User_info";
                    if (filter.Length > 0)
                        sql += "  where  " + filter;
                    if (OrderFields.Length > 0)
                    {
                        if (isDesc)
                            sql += "   ORDER BY  " + OrderFields + "  DESC";
                        else
                            sql += "   ORDER BY  " + OrderFields;
                    }
                    return dbContext.GetListBySql<Entity.User_info>(sql, null).ToList();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取登陆用户数据");
                    return null;
                }
            }
        }
        public Entity.User_info Get(int UserId)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                return dbContext.Get<Entity.User_info>(UserId, null);
            }
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public Entity.User_info GetUser(string userCode)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { UserCode = userCode };
                var user = dbContext.GetList<Entity.User_info>(precidate, null).FirstOrDefault();
                return user;
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="user"></param>
        /// <param name="update_user">更新用户</param>
        /// <returns></returns>
        public string Update(Entity.User_info user)
        {
            if (user == null || user.UserId <= 0) return "数据有误";
            var oldUser = this.Get(user.UserId);
            if (oldUser == null) return "用户不存在";
            oldUser.UserId = user.UserId;
            oldUser.UserName = user.UserName;
            oldUser.UserCode = user.UserCode;
            oldUser.UserLogPwd = user.UserLogPwd;
            oldUser.UserPhone = user.UserPhone;

            oldUser.Wx_UserID = user.Wx_UserID;
            oldUser.EnterpriseID = user.EnterpriseID;
            oldUser.EnterpriseCode = user.EnterpriseCode;
            oldUser.RoleID = user.RoleID;

            oldUser.update_time = DateTime.Now;
            oldUser.update_user = user.update_user;
            oldUser.create_time = user.create_time;
            oldUser.create_user = user.create_user == null ? "SYSTEM" : user.create_user;
            oldUser.version = user.version + 1;

            using (var dbContext = Repository.DbContext.Create())
            {
                bool success = dbContext.Update(oldUser);
                if (success)
                    return string.Empty;
                return "更新失败";
            }
        }
        public string Update(Entity.User_infoQuery qu, string openUser)
        {
            if (qu.UserName == "admin")
                return "超级管理员不允许修改";
            var user = this.Get(qu.UserId);
            user.UserPhone = qu.UserPhone;
            user.update_user = openUser;
            user.RoleID = qu.RoleID;
            return this.Update(user);
        }
        /// <summary>
        /// 是否存用户代码
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public bool ExistsCode(string UserCode)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { UserCode = UserCode };
                return dbContext.Count<Entity.User_info>(precidate, null) > 0;
            }
        }
        /// <summary>
        /// 是否存用户名
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public bool ExistsName(string UserName)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { UserName = UserName };
                return dbContext.Count<Entity.User_info>(precidate, null) > 0;
            }
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="enter_id">企业id</param>
        /// <param name="userName">用户名</param>
        /// <param name="userCode">用户代码</param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public PageList<Entity.User_infoQuery> GetRoleQuery(int enter_id, string userName, string userCode, Entity.BaseFilter filter)
        {
            dynamic predicate = new System.Dynamic.ExpandoObject();
            string sql = @"   SELECT us.UserId,us.UserName,us.UserCode,us.UserPhone,e.Enterprise_Name,us.RoleID,r.RoleName,us.create_time,us.create_user,us.update_time,us.update_user,us.version FROM User_info us 
LEFT JOIN dbo.Role r ON us.RoleID=r.ID 
LEFT JOIN dbo.Enterprise e ON us.EnterpriseID=e.ID {0}";
            sql = String.Format(sql, "WHERE us.UserName LIKE '%" + userName + "%' AND us.UserCode LIKE '%" + userCode + "%' ");
            sql = enter_id > 0 ? sql + " and  us.EnterpriseID= " + enter_id : sql;
            sql += " ORDER BY us." + filter.sort + (filter.asc ? string.Empty : "  DESC"); ;
            using (var dbContext = Repository.DbContext.Create())
            {
                var data = dbContext.GetListBySql<Entity.User_infoQuery>(sql, predicate, filter.page - 1, filter.rows);
                return data;
            }
        }

        public int Add(User_infoQuery qu, int EnterpriseID, string EnterpriseCode, string openUser)
        {
            int newId = -1;
            if (qu.UserName == "admin")
                return newId;
            if (this.ExistsCode(qu.UserCode) || this.ExistsName(qu.UserName))
                return newId;

            Entity.User_info model = new Entity.User_info();
            var bll = new Core.BLL.LoginUser();
            model.UserName = qu.UserName;
            model.UserCode = qu.UserCode;
            model.UserPhone = qu.UserPhone;
            model.Wx_UserID = 0;
            model.RoleID = qu.RoleID;
            model.UserLogPwd = bll.EncodePasswod(model.UserCode, model.UserCode);
            model.EnterpriseCode = EnterpriseCode;
            model.EnterpriseID = EnterpriseID;
            model.update_time = DateTime.Now;
            model.update_user = openUser;
            model.create_time = DateTime.Now;
            model.create_user = openUser;
            using (var dbContext = Repository.DbContext.Create())
            {
                newId = (int)dbContext.Insert<Entity.User_info>(model);
            }
            return newId;
        }

        /// <summary>
        ///删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string Delete(int Id)
        {

            using (var dbContext = Repository.DbContext.Create())
            {
                var user = this.Get(Id);
                if (user.UserName == "admin")
                    return "超级管理员不允许删除";
                var predicate = new { UserId = Id };
                bool success = dbContext.Delete<Entity.User_info>(predicate, null);
                if (success)
                    return string.Empty;
                return "删除失败";
            }
        }
    }
}
